package ma.co.ba.BusinessApplications.presentation.controllers.tickets;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ma.co.ba.BusinessApplications.business.clients.ClientBusiness;
import ma.co.ba.BusinessApplications.business.clients.ContactBusiness;
import ma.co.ba.BusinessApplications.business.commons.CommonBusiness;
import ma.co.ba.BusinessApplications.business.products.ProduitBusiness;
import ma.co.ba.BusinessApplications.business.tickets.TicketBusiness;
import ma.co.ba.BusinessApplications.business.utilisateurs.UtilisateurBusiness;
import ma.co.ba.BusinessApplications.model.Client;
import ma.co.ba.BusinessApplications.model.Contact;
import ma.co.ba.BusinessApplications.model.Criticite;
import ma.co.ba.BusinessApplications.model.DAT;
import ma.co.ba.BusinessApplications.model.Etat;
import ma.co.ba.BusinessApplications.model.EtatTicket;
import ma.co.ba.BusinessApplications.model.FamilleDeProduit;
import ma.co.ba.BusinessApplications.model.Produit;
import ma.co.ba.BusinessApplications.model.Utilisateur;
import ma.co.ba.BusinessApplications.presentation.components.ClientEditor;
import ma.co.ba.BusinessApplications.presentation.components.ContactEditor;
import ma.co.ba.BusinessApplications.presentation.components.CriticiteEditor;
import ma.co.ba.BusinessApplications.presentation.components.EtatEditor;
import ma.co.ba.BusinessApplications.presentation.components.FamilleDeProduitEditor;
import ma.co.ba.BusinessApplications.presentation.components.ProduitEditor;
import ma.co.ba.BusinessApplications.presentation.components.UtilisateurEditor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/tickets")
public class TicketController {

	@Autowired
	CommonBusiness commonBusiness;
	@Autowired
	TicketBusiness ticketBusiness;
	@Autowired
	ClientBusiness clientBusiness;
	@Autowired
	ContactBusiness contactBusiness;
	@Autowired
	ProduitBusiness produitBusiness;
	@Autowired
	UtilisateurBusiness utilisateurBusiness;

	@Autowired
	ClientEditor clientEditor;
	@Autowired
	ContactEditor contactEditor;
	@Autowired
	ProduitEditor produitEditor;
	@Autowired
	FamilleDeProduitEditor familleDeProduitEditor;
	@Autowired
	CriticiteEditor criticiteEditor;
	@Autowired
	EtatEditor etatEditor;
	@Autowired
	UtilisateurEditor utilisateurEditor;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
		binder.registerCustomEditor(Client.class, this.clientEditor);
		binder.registerCustomEditor(Contact.class, this.contactEditor);
		binder.registerCustomEditor(Produit.class, this.produitEditor);
		binder.registerCustomEditor(FamilleDeProduit.class,
				this.familleDeProduitEditor);
		binder.registerCustomEditor(Criticite.class, this.criticiteEditor);
		binder.registerCustomEditor(Etat.class, this.etatEditor);
		binder.registerCustomEditor(Utilisateur.class, this.utilisateurEditor);
	}

	@RequestMapping(method = RequestMethod.GET)
	public String ticketsPage(ModelMap model) {
		List<DAT> ticketsList = ticketBusiness.getAllDats();
		model.addAttribute("ticketsList", ticketsList);
		return "tickets";
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String newTicketPage(ModelMap model) {

		return "newTicket";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public String addNewTicket(@ModelAttribute("newTicket") DAT newTicket,
			ModelMap model) {

		Etat etat = null;
		if (newTicket.isAffect()) {
			etat = ticketBusiness.getEtatByValue(EtatTicket.Affecte.toString());
		} else {
			etat = ticketBusiness.getEtatByValue(EtatTicket.NonAffecte
					.toString());
		}
		newTicket.setEtat(etat);
		commonBusiness.saveEntity(newTicket);
		return "redirect:/tickets";
	}

	@RequestMapping(value = "/me", method = RequestMethod.GET)
	public String mesTicketsPage(ModelMap model, Principal principal) {

		List<DAT> ticketsList = null;
		String name = principal.getName();
		if (name == null || "admin".equals(name) || "resp".equals(name)
				|| "cons".equals(name))
			return "activites";
		else {
			ticketsList = utilisateurBusiness.getTicketsByUsername(name);
		}

		model.addAttribute("ticketsList", ticketsList);
		return "mesTickets";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String deleteTicket(@RequestParam("id") int ticketId, ModelMap model) {
		DAT dat = ticketBusiness.getDATById(ticketId);
		commonBusiness.deleteEntity(dat);
		return "redirect:/tickets";
	}

	// @ModelAttribute("ticketsList")
	// public List<DAT> getAllTicketList() {
	// return ticketBusiness.getAllDats();
	// }

	@ModelAttribute("newTicket")
	public DAT getNewTicket() {
		return new DAT();
	}

	@ModelAttribute("clientsList")
	public Map<String, String> getClientList() {
		List<Client> activeClients = null;
		activeClients = clientBusiness.getActiveClients();
		Map<String, String> clientsList = new HashMap<String, String>();
		if (activeClients != null) {
			for (Client client : activeClients) {
				clientsList.put(client.getId() + "", client.getNomClient());
			}
		}
		return clientsList;
	}

	@ModelAttribute("contactsList")
	public Map<String, String> getContactsList() {
		List<Contact> activeContacts = null;

		activeContacts = contactBusiness.getActiveContacts();
		Map<String, String> contactsList = new HashMap<String, String>();

		if (activeContacts != null) {
			for (Contact contact : activeContacts) {
				contactsList.put(contact.getId() + "", contact.getNom() + " "
						+ contact.getPrenom());
			}
		}
		return contactsList;
	}

	@ModelAttribute("pfList")
	public Map<String, String> getProductFamilyList() {
		List<FamilleDeProduit> familleDeProduits = null;

		familleDeProduits = produitBusiness.getAllFamilleProduits();
		Map<String, String> pfList = new HashMap<String, String>();

		if (familleDeProduits != null) {
			for (FamilleDeProduit familleDeProduit : familleDeProduits) {
				pfList.put(familleDeProduit.getId() + "",
						familleDeProduit.getProductFamilyName());
			}
		}
		return pfList;
	}

	@ModelAttribute("productsList")
	public Map<String, String> getProductsList() {
		List<Produit> products = null;

		products = produitBusiness.getAllProduits();
		Map<String, String> productsList = new HashMap<String, String>();

		if (products != null) {
			for (Produit produit : products) {
				productsList
						.put(produit.getId() + "", produit.getProductName());
			}
		}
		return productsList;
	}

	@ModelAttribute("consultantsList")
	public Map<String, String> getConsultantsList() {
		List<Utilisateur> consultants = null;
		consultants = utilisateurBusiness.getActiveConsultants();
		Map<String, String> consultantsList = new HashMap<String, String>();
		if (consultants != null) {
			for (Utilisateur consultant : consultants) {
				consultantsList.put(consultant.getId() + "",
						consultant.getNom() + " " + consultant.getPrenom());
			}
		}
		return consultantsList;
	}

	@ModelAttribute("criticitesList")
	public Map<String, String> getCriticitesList() {
		List<Criticite> criticites = null;
		criticites = ticketBusiness.getAllCriticites();
		Map<String, String> criticitesList = new HashMap<String, String>();
		if (criticites != null) {
			for (Criticite criticite : criticites) {
				criticitesList.put(criticite.getId() + "",
						criticite.getLibelleCriticite());
			}
		}
		return criticitesList;
	}

	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
}
